package com.it.travel.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.it.travel.common.ConstEnum;
import com.it.travel.dao.OrderMapper;
import com.it.travel.dto.admin.AdminOrderDTO;
import com.it.travel.pojo.Order;
import com.it.travel.service.admin.AdminOrderService;
import com.it.travel.vo.Params.PageParams;
import com.it.travel.vo.Result;
import com.it.travel.vo.admin.AdminOrderVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class AdminOrderServiceImpl implements AdminOrderService {
    @Resource
    private OrderMapper orderMapper;

    @Override
    public Result<?> getInfoPage(PageParams pageParams) {
        Integer total = 0;
        if ("".equals(pageParams.getKeywords())) {
            LambdaQueryWrapper<Order> wrapper = Wrappers.lambdaQuery();
            wrapper.like(Order::getOrderNum, pageParams.getKeywords());
            total = orderMapper.selectCount(wrapper); // 为空字符串查出所有数量
            pageParams.setKeywords(null);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        pageParams.setIndex((pageParams.getIndex() - 1) * pageParams.getSize());
        HashMap<String, Object> map = new HashMap<>();
        List<AdminOrderDTO> orderDTOList = orderMapper.selectAdminOrderPage(pageParams.getIndex(), pageParams.getSize(), pageParams.getKeywords(),pageParams.getRole(),pageParams.getUserId());
        ArrayList<AdminOrderVO> list = new ArrayList<>();
        for (AdminOrderDTO adminOrderDTO : orderDTOList) {
            AdminOrderVO adminOrderVO = new AdminOrderVO();
            BeanUtil.copyProperties(adminOrderDTO, adminOrderVO);
            Date date = new Date(adminOrderDTO.getCreateTime());
            if (adminOrderDTO.getDeliveryTime() != null) {
                String format1 = simpleDateFormat.format(new Date(adminOrderDTO.getDeliveryTime()));// 发货时间
                adminOrderVO.setFormatDelivery(format1);
            } else {
                adminOrderVO.setFormatDelivery("未发货");
            }
            String format = simpleDateFormat.format(date);
            adminOrderVO.setFormatTime(format);
            if (ConstEnum.PAY_zfb.getNum().equals(adminOrderDTO.getPayType())) {// 支付方式
                adminOrderVO.setPay("支付宝");
            } else if (ConstEnum.PAY_weixin.getNum().equals(adminOrderDTO.getPayType())) { // else if 可能值为null 就不走这2个
                adminOrderVO.setPay("微信");
            }
            if (ConstEnum.PAY_STATUS.getNum().equals(adminOrderDTO.getOrderStatus())) { // 用elseis
                adminOrderVO.setFormatOrderStatus("已支付");
            } else {// else 可能 值为null
                adminOrderVO.setFormatOrderStatus("未支付");
            }
            list.add(adminOrderVO);
        }
        System.out.println("total = " + total);
        System.out.println("list = " + list);
        map.put("total", total);
        map.put("orderList", list);
        return Result.success(map, "获取数据成功");
    }

    @Override
    public Result<?> updateOrderStatus(Integer id) {  // 进行发货操作
        Order order = orderMapper.selectById(id);
        if (ConstEnum.PAY_NOT_STATUS.getNum().equals(order.getOrderStatus())) { // 未支付的话就不让发货
            return Result.error(null, "订单还未支付");
        }
        if (order.getDeliveryTime() != null) {
            return Result.error(null, "已发货！");
        }
        order.setDeliveryTime(System.currentTimeMillis()); // 发货时间
        order.setUpdateTime(System.currentTimeMillis()); // 订单更新时间
        order.setFlishTime(System.currentTimeMillis()); // 订单完成时间
        int i = orderMapper.updateById(order);
        if (i > 0) {
            return Result.success(null, "发货成功");
        }
        return Result.error(null, "发货失败");
    }
}
